#!/bin/sh
#
# Copyright (C) 2014, 2015, 2023  Etersoft
# Copyright (C) 2014, 2015, 2023  Vitaly Lipatov <lav@etersoft.ru>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#

__epm_apt_set_lists_pkg()
{
    # apt-dpkg
    pkg="Packages"

    LISTS='/var/lib/apt/lists'
    if [ "$BASEDISTRNAME" = "alt" ] ; then
        pkg="pkglist"
        # see update-kernel: Use Dir::State::lists for apt update freshness check (ALT bug 46987)
        eval "$(apt-config shell LISTS Dir::State::lists/f)"
    fi
}

# initially copied from update-kernel
# print number of days and error status if number of days more than 0
__epm_check_apt_db_days()
{
    local pkg
    local pkglists
    __epm_apt_set_lists_pkg
    pkglists=$(find $LISTS -name "*_$pkg*" -ctime +1 2>/dev/null)
    if [ -z "$pkglists" ] ; then
        # note: duplicate __is_repo_info_downloaded
        pkglists=$(find $LISTS -name "*_$pkg*" 2>/dev/null)
        [ -n "$pkglists" ] && return
        echo "never downloaded"
        return 1
    fi

    local i t
    local ts=0
    # set ts to newest file ctime
    # shellcheck disable=SC2044
    for i in $(find $LISTS -name "*_$pkg*" 2>/dev/null); do
        t=$(stat -c%Z "$i")
        [ "$t" -gt "$ts" ] && ts=$t
    done

    if [ "$ts" -gt 0 ]; then
        # shellcheck disable=SC2017
        local now=$(date +%s)
        local days="$(( (now - ts) / (60 * 60 * 24) ))"
        [ "$days" = "0" ] && return 0
        [ "$days" = "1" ] && echo "1 day old" && return 1
        echo "$days days old"
    else
        # no any pkglist
        echo "stalled"
    fi
    return 1
}

__epm_touch_apt_pkg()
{
    local pkg
    __epm_apt_set_lists_pkg
    # ordinal package file have date of latest upstream change, not latest update, so update fake file
    sudorun touch "$LISTS/eepm-fake_$pkg"
}

__epm_touch_pkg()
{
    case $PMTYPE in
        apt-*)
            __epm_touch_apt_pkg
            ;;
    esac
}

# check if we need initial update
__is_repo_info_downloaded()
{
    case $PMTYPE in
        apt-*)
            local pkg
            __epm_apt_set_lists_pkg
            local pkglists
            pkglists=$(find $LIST -name "*_$pkg*" 2>/dev/null)
            [ -n "$pkglists" ] || return 1
            ;;
        *)
            ;;
    esac
    return 0
}

__is_repo_info_uptodate()
{
    case $PMTYPE in
        apt-*)
            __epm_check_apt_db_days >/dev/null
            ;;
        *)
            ;;
    esac
    return 0
}

update_repo_if_needed()
{
    local days

    # for apt only
    case $PMTYPE in
        apt-*)
            ;;
        *)
            return
            ;;
    esac

    days="$(__epm_check_apt_db_days)" && return
    warning "APT database is $days, please run 'epm update'!"

    # TODO: enable __is_repo_info_downloaded

    return
    # check if we need skip update checking
    #if [ "$1" = "soft" ] && ! set_sudo nofail ; then
    #    # if sudo requires a password, skip autoupdate
    #    info "can't use sudo, so skip repo status checking"
    #    return 1
    #fi

    cd / || fatal
    if ! __is_repo_info_downloaded || ! __is_repo_info_uptodate ; then
        load_helper epm-update
        # FIXME: cleans!!!
        epm_update
    fi
    cd - >/dev/null || fatal

}

# save manually installed packages
save_installed_packages()
{
    [ -d /var/lib/rpm ] || return 0
    estrlist list "$@" | sudorun tee /var/lib/rpm/EPM-installed >/dev/null
}

check_manually_installed()
{
    [ -r /var/lib/rpm/EPM-installed ] || return 1
    grep -q -- "^$1\$" /var/lib/rpm/EPM-installed
}

skip_manually_installed()
{
    local i
    for i in "$@" ; do
        check_manually_installed "$i" && continue
        echo "$i"
    done
}
